<template>
	<view class="user-logistics-form">
		<view class="user-logistics-form-start">
			<view class="user-logistics-form-tit">
				<image src="https://master.shengejiaju.cn/web/static/icons/icon_start@2x.png"></image>
				始发站
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">物流名称</text>
				<input placeholder="填写物流名称" type="text" v-model="addressForm.company_name" />
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">联系人</text>
				<input placeholder="填写联系人姓名" type="text" v-model="addressForm.start_consignee" />
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">联系电话</text>
				<input placeholder="填写收货人手机号" type="number"  v-model="addressForm.start_mobile"/>
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">座机号</text>
				<input placeholder="填写座机号" type="text" v-model="addressForm.start_landline" />
			</view>
			<view class="user-logistics-form-cell select">
				<text class="label">选择所在地区</text>
				<view :class="['select-region',{select: start_region_name}]" @click="selectRegion('start')">
					{{start_region_name?start_region_name:'请选择所在地区'}}
				</view>
			</view>
			<view class="user-logistics-form-cell textarea">
				<text class="label">详细地址</text>
				<textarea placeholder="填写道路、门牌号、小区等" v-model="addressForm.start_address"></textarea>
			</view>
		</view>
		<view class="user-logistics-form-start">
			<view class="user-logistics-form-tit">
				<image src="https://master.shengejiaju.cn/web/static/icons/icon_end@2x.png"></image>
				终点站
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">联系人</text>
				<input placeholder="填写联系人姓名" type="text" v-model="addressForm.end_consignee" />
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">联系电话</text>
				<input placeholder="填写收货人手机号" type="number"  v-model="addressForm.end_mobile"/>
			</view>
			<view class="user-logistics-form-cell">
				<text class="label">座机号</text>
				<input placeholder="填写座机号" type="text" v-model="addressForm.end_landline" />
			</view>
			<view class="user-logistics-form-cell select">
				<text class="label">选择所在地区</text>
				<view :class="['select-region',{select: end_region_name}]" @click="selectRegion('end')">
					{{end_region_name?end_region_name:'请选择所在地区'}}
				</view>
			</view>
			<view class="user-logistics-form-cell textarea">
				<text class="label">详细地址</text>
				<textarea placeholder="填写道路、门牌号、小区等" v-model="addressForm.end_address"></textarea>
			</view>
		</view>
		
		<view class="user-logistics-btn" @click="submit">
			<text>{{address_id?'保存编辑':'立即添加'}}</text>
		</view>
		
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" cancelColor="#666" confirmColor="#2073F5"></simple-address>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				address_id: '',
				type: '',
				start_region_name: '',
				end_region_name: '',
				addressForm: {
					company_name: '',
					start_consignee: '',
					start_province_id: '',
					start_city_id: '',
					start_district_id: '',
					start_address: '',
					start_mobile: '',
					start_landline: '',
					end_consignee: '',
					end_mobile: '',
					end_province_id: '',
					end_city_id: '',
					end_district_id: '',
					end_address: '',
					end_landline: ''
				},
				cityPickerValueDefault: [0, 0, 0],
			};
		},
		onLoad(options) {
			if(options.address_id){
				this.address_id = options.address_id
				uni.setNavigationBarTitle({
					title: '编辑商家物流'
				})
				this.init()
			}
		},
		methods: {
			init(){
				this.$api.LogisticsList({address_id: this.address_id}).then(res => {
					this.start_region_name = res.datas.data[0].start_province + ',' + res.datas.data[0].start_city + ',' + res.datas.data[0].start_district
					this.end_region_name = res.datas.data[0].end_province + ',' + res.datas.data[0].end_city + ',' + res.datas.data[0].end_district
					this.addressForm = {
						address_id: res.datas.data[0].address_id,
						company_name: res.datas.data[0].company_name,
						start_consignee: res.datas.data[0].start_consignee,
						start_province_id: res.datas.data[0].start_province_id,
						start_city_id: res.datas.data[0].start_city_id,
						start_district_id: res.datas.data[0].start_district_id,
						start_address: res.datas.data[0].start_address,
						start_mobile: res.datas.data[0].start_mobile,
						start_landline: res.datas.data[0].start_landline,
						end_consignee: res.datas.data[0].end_consignee,
						end_mobile: res.datas.data[0].end_mobile,
						end_province_id: res.datas.data[0].end_province_id,
						end_city_id: res.datas.data[0].end_city_id,
						end_district_id: res.datas.data[0].end_district_id,
						end_address: res.datas.data[0].end_address,
						end_landline: res.datas.data[0].end_landline
					}
				})
			},
			selectRegion(type){
				this.type = type
				if(this.address_id){
					if(type==='start'){
						let arr = this.start_region_name.split(',')
						let index = this.$refs.simpleAddress.queryIndex(arr, 'label')
						this.cityPickerValueDefault = index.index
					}else{
						let arr = this.end_region_name.split(',')
						let index = this.$refs.simpleAddress.queryIndex(arr, 'label')
						this.cityPickerValueDefault = index.index
					}
				}
				this.$refs.simpleAddress.open();
			},
			onConfirm(e){
				console.log(e)
				if(this.type==='start'){
					this.start_region_name = e.labelArr.join(',')
					this.addressForm.start_province_id = e.provinceCode
					this.addressForm.start_city_id = e.cityCode
					this.addressForm.start_district_id = e.areaCode
				}else{
					this.end_region_name = e.labelArr.join(',')
					this.addressForm.end_province_id = e.provinceCode
					this.addressForm.end_city_id = e.cityCode
					this.addressForm.end_district_id = e.areaCode
				}
			},
			submit(){
				if(this.address_id){
					this.$api.UpdateLogistics(this.addressForm).then(res => {
						uni.showToast({
							icon: 'none',
							title: '编辑成功'
						})
						setTimeout(() => {
							uni.navigateBack()
						},1500)
					})
				}else{
					this.$api.UpLogistics(this.addressForm).then(res => {
						uni.showToast({
							icon: 'none',
							title: '添加成功'
						})
						setTimeout(() => {
							uni.navigateBack()
						},1500)
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F7F7F7;
	}
	.user-logistics-form{
		padding-top: 1rpx;
		padding-bottom: 140rpx;
	}
	.user-logistics-form-start{
		background-color: #FFF;
		margin-top: 20rpx;
		.user-logistics-form-tit{
			display: flex;
			align-items: center;
			padding: 30rpx;
			font-size: 32rpx;
			font-weight: bold;
			image{
				width: 48rpx;
				height: 60rpx;
				margin-right: 30rpx;
			}
		}
		.user-logistics-form-cell{
			display: flex;
			padding: 30rpx;
			position: relative;
			&::after{
				content: '';
				position: absolute;
				height: 2rpx;
				left: 30rpx;
				right: 30rpx;
				background-color: #F5F5F5;
				bottom: 0;
			}
			&:last-child::after{
				display: none;
			}
			input{
				flex: 1;
				min-width: 0;
				text-align: right;
				margin-left: 30rpx;
			}
			textarea{
				flex: 1;
				min-width: 0;
				text-align: right;
				margin-left: 30rpx;
				height: 80rpx;
				line-height: 40rpx;
			}
			.select-region{
				flex: 1;
				min-width: 0;
				text-align: right;
				color: #888;
				margin-left: 30rpx;
				&.select{
					color: #333;
				}
			}
		}
	}
	.user-logistics-btn{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 120rpx;
		background-color: #FFF;
		display: flex;
		align-items: center;
		border-top: solid 2rpx #EEE;
		text{
			flex: 1;
			display: block;
			height: 90rpx;
			line-height: 90rpx;
			margin: 0 75rpx;
			background-color: #2073f5;
			color: #FFF;
			font-size: 32rpx;
			text-align: center;
			border-radius: 8rpx;
		}
	}
</style>
